package com.pangyangyang.elasticsearch.plugin.handler;

import com.pangyangyang.elasticsearch.plugin.jdbc.query.QueryDataEntity;
import com.pangyangyang.elasticsearch.plugin.util.MessageRiverConstants;
import com.pangyangyang.elasticsearch.plugin.river.Build;
import com.pangyangyang.elasticsearch.plugin.river.MessageRiverSettings;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;

/**
 * Created by yangwang on 2015/1/15.
 */
public abstract class AbstractEventHandler implements EventHandler {

    protected Client client;
    protected ESLogger logger = ESLoggerFactory.getLogger(Build.LOGGER_NAME);

    protected String indexString;
    protected String typeString;
    protected String destination;
    protected MessageRiverSettings settings;

    public AbstractEventHandler(Client client, MessageRiverSettings settings) {
        this.client = client;
        this.settings = settings;
        this.destination = settings.getString(MessageRiverConstants.SETTING_KEY_COMMON_DESTINATION);
    }

    @Override
    public void start() {
        indexString = settings.getString(MessageRiverConstants.SETTING_KEY_COMMON_INDEX);
        typeString = settings.getString(MessageRiverConstants.SETTING_KEY_COMMON_TYPE);
        doStart();
    }

    protected abstract void doStart();

    @Override
    public String getDestination() {
        return this.destination;
    }

    protected boolean indexDocument(QueryDataEntity entity){
        return indexDocument(indexString, typeString, entity);
    }

    protected boolean indexDocument(String jsonDocument){
        return indexDocument(indexString, typeString, jsonDocument);
    }

    protected boolean indexDocument(String index, String type, QueryDataEntity entity){
        logger.debug("indexing document {}", entity);
        IndexRequestBuilder builder =
                client.prepareIndex(index, type).setSource(entity.toJsonDocument());

        if(entity.getId() != null){
            builder.setId(entity.getId());
        }

        IndexResponse response = builder.execute().actionGet();
        return response.getVersion() > 0;
    }

    protected boolean indexDocument(String index, String type, String jsonDocument){
        logger.debug("indexing document {}", jsonDocument);
        IndexRequestBuilder builder =
                client.prepareIndex(index, type).setSource(jsonDocument);
        IndexResponse response = builder.execute().actionGet();
        return response.getVersion() > 0;
    }

    /**
     *
     * @param id
     * @return 找到true，没有找到false
     */
    protected boolean deleteDocument( String id) {

        return deleteDocument(indexString, typeString, id);
    }

    /**
     *
     * @param id
     * @return 找到true，没有找到false
     */
    protected boolean deleteDocument(String index, String type, String id){

        DeleteResponse response = client.prepareDelete(index, type, id)
                .execute()
                .actionGet();

        return response.isFound();
    }

    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }
}
